import request from '@/utils/request'

export const videoApi = {
  // 上传文件到OSS
  uploadFile(file) {
    const formData = new FormData()
    formData.append('file', file)
    return request({
      url: '/file/upload',
      method: 'post',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  // 创建视频记录
  createVideo(videoData) {
    return request({
      url: '/admin/video/create',
      method: 'post',
      data: {
        title: videoData.title,
        description: videoData.description,
        file_path: videoData.filePath,
        thumbnail_path: videoData.thumbnailPath,
        is_public: videoData.isPublic ? '1' : '0',
        tags: videoData.tags,
        duration: videoData.duration
      }
    })
  },

  // 获取视频列表
  getVideoList(params) {
    return request({
      url: '/admin/video/all',
      method: 'post',
      data: {
        id: params.id,
        page: params.page,
        pageSize: params.pageSize,
        status: params.status,
        title: params.title,
        tagIds: params.tagIds
      }
    }).then(res => {
      return res
    }).catch(err => {
      throw err
    })
  },

  // 获取作者的其他视频
  getAuthorVideos(params) {
    return request({
      url: '/admin/video/author',
      method: 'post',
      data: {
        userId: params.userId,
        currentVideoId: params.currentVideoId,
        page: params.page || 1,
        pageSize: params.pageSize || 10
      }
    })
  },

  // 获取作者信息
  getAuthorInfo(userId) {
    return request({
      url: `/user/user/getUserInfo/${userId}`,
      method: 'get'
    })
  },

  // 删除视频
  deleteVideo(videoId) {
    return request({
      url: `/admin/video/delete/${videoId}`,
      method: 'delete'
    })
  },

  // 添加点赞/取消点赞
  likeVideo(params) {
    return request({
      url: '/admin/video/like',
      method: 'post',
      data: params
    })
  },

  // 增加播放量
  addVideoCount(videoId) {
    return request({
      url: `/admin/video/addCount/${videoId}`,
      method: 'post'
    }).then(res => {
      return res
    }).catch(err => {
      throw err
    })
  },

  // 添加收藏
  addFavorite(videoId) {
    return request({
      url: '/videoFavorites/add',
      method: 'post',
      data: {
        videoId: videoId
      }
    })
  },

  // 取消收藏
  deleteFavorite(videoId) {
    return request({
      url: `/videoFavorites/delete/${videoId}`,
      method: 'post'
    })
  },

  // 获取收藏状态
  getIsFavorites(videoId) {
    return request({
      url: '/videoFavorites/getIsFavorites',
      method: 'get',
      params: {
        videoId: videoId
      }
    })
  },

  // 获取点赞状态
  getVideoLikes(videoId) {
    return request({
      url: '/videoLikes/getVideoLikes',
      method: 'post',
      data: {
        videoId: videoId
      }
    })
  },

  // 添加点赞
  addLike(videoId) {
    return request({
      url: '/videoLikes/add',
      method: 'post',
      data: {
        videoId: videoId
      }
    })
  },

  // 取消点赞
  deleteLike(videoId) {
    return request({
      url: '/videoLikes/delete',
      method: 'delete',
      data: {
        videoId: videoId
      }
    })
  },

  // 获取关注状态
  getFollowStatus(userId) {
    return request({
      url: '/userFollow/getFoller',
      method: 'get',
      params: {
        uid: userId
      }
    })
  },

  // 添加关注
  addFollow(userId) {
    return request({
      url: '/userFollow/add',
      method: 'post',
      data: {
        followingId: userId,
        followerId: JSON.parse(localStorage.getItem('user')).id
      }
    })
  },

  // 取消关注
  deleteFollow(userId) {
    return request({
      url: '/userFollow/del',
      method: 'delete',
      data: {
        followingId: userId,
        followerId: JSON.parse(localStorage.getItem('user')).id
      }
    })
  }
} 